<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">
          <!-- <h2 class="brand-info__text">潍坊学院</h2> -->

          <img src="~@/assets/img/hhh.jpg" alt="logo">
          <p class="brand-info__intro">At Uptown</p>
        </div>
        <div class="login-main">
          <el-tabs :stretch=true v-model="activeName">
            <el-tab-pane label="账号密码登录" name="first">
              <el-form :model="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmit()" ref="dataForm"
                       status-icon>
                <el-form-item prop="userName">
                  <el-input placeholder="帐号" prefix-icon="el-icon-info" v-model="dataForm.userName"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input placeholder="密码" prefix-icon="el-icon-news" type="password"
                            v-model="dataForm.password"></el-input>
                </el-form-item>
                <el-form-item prop="captcha">
                  <el-row :gutter="20">
                    <el-col :span="14">
                      <el-input v-model="dataForm.captcha" placeholder="验证码">
                      </el-input>
                    </el-col>
                    <el-col :span="10" class="login-captcha">
                      <img :src="captchaPath" @click="getCaptcha()" alt="">
                    </el-col>
                  </el-row>
                </el-form-item>
                <el-form-item>
                  <el-button class="login-btn-submit" type="primary" @click="dataFormSubmit()">登录</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="手机号登录" name="second">
              <el-form :model="phoneDataFrom" :rules="phoneRule" @keyup.enter.native="phoFromSubmit()"
                       ref="phoneDataFrom"
                       status-icon>
                <el-form-item prop="phoneNumber">
                  <el-input placeholder="手机号" prefix-icon="el-icon-mobile-phone"
                            v-model="phoneDataFrom.phoneNumber"></el-input>
                </el-form-item>
                <el-form-item prop="code">
                  <captcha v-model="phoneDataFrom.code" :phoneNumber="phoneDataFrom.phoneNumber"/>
                </el-form-item>
                <el-form-item>
                  <el-button @click="phoFromSubmit()" class="login-btn-submit" type="primary">登录</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {getUUID} from '../../utils'
  import captcha from '@/components/smsCaptcha/captcha'

  export default {
    components: {
      captcha
    },
    data () {
      return {
        activeName: 'first',
        dataForm: {
          userName: '',
          password: '',
          uuid: '',
          captcha: ''
        },
        phoneDataFrom: {
          phoneNumber: '',
          code: ''
        },
        dataRule: {
          userName: [
            {required: true, message: '帐号不能为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ],
          captcha: [
            {required: true, message: '验证码不能为空', trigger: 'blur'}
          ]
        },
        phoneRule: {
          phoneNumber: [
            {required: true, message: '手机号不能为空', trigger: 'blur'}
          ],
          code: [
            {required: true, message: '验证码不能为空', trigger: 'blur'},
            {min: 6, message: '填写正确的验证码'}
          ]
        },
        captchaPath: '',
        captchaUrl: ''
      }
    },
    created () {
      this.getCaptcha()
    },
    methods: {
      // 提交表单
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: 'login',
              method: 'post',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
              },
              data: this.$http.adornData({
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'uuid': this.dataForm.uuid,
                'captcha': this.dataForm.captcha
              }, false, 'form')
            }).then(({data}) => {
              this.$cookie.set('token', data.token)
              this.$router.replace({name: 'home'})
            }).catch(err=>{
              this.getCaptcha()
            })
          }
        })
      },
      //提交手机号登录表单
      phoFromSubmit() {
        this.$refs.phoneDataFrom.validate((valid) => {
          if (valid) {
            this.$http({
              url: '/phone/login',
              method: 'post',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
              },
              data: this.$http.adornData({
                'phone': this.phoneDataFrom.phoneNumber,
                'code': this.phoneDataFrom.code
              }, false, 'form')
            }).then(({data}) => {
              this.$cookie.set('token', data.token)
              this.$router.replace({name: 'home'})
            })
          } else {
            console.log('invalid')
          }
        })
      },
      getCaptcha () {
        this.dataForm.uuid = getUUID()
        this.captchaPath = this.$http.adornUrl(`/api/captcha.jpg?uuid=${this.dataForm.uuid}`)
      }
    }
  }
</script>

<style lang="scss">
  .site-wrapper.site-page--login {
    width: 100%;
    min-width: 600px;

    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(38, 50, 56, .1);
    overflow: hidden;
    &:before {
      position: fixed;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      content: "";
      background-image: url(~@/assets/img/login_bg.jpg);
      background-position: 0%;
      background-repeat: no-repeat;
      background-color: #2b7db0;
    }
    .site-content__wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      padding: 0;
      margin: 0;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: transparent;
    }
    .site-content {
      min-height: 100%;
      padding: 30px 500px 30px 30px;
    }
    .brand-info {
      // margin: 280px 100px 0 90px;
      height: 3.9375rem;
      color: #fff;
      img {
        width: 63px;
        height: 63px;
        float: left;
        margin-right: 5px;
        border-radius: 50%;
      }
    }
    .brand-info__text {
      margin: 0 0 22px 0;
      font-size: 48px;
      font-weight: 400;
      text-transform: uppercase;
    }
    .brand-info__intro {
      // font-family: STKaiti;
      height: 3.9375rem;
      margin: 0;
      font-size: 38px;
      line-height: 1.58;
      opacity: 1.6;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .login-main {
      position: absolute;
      top: 0;
      right: 0;
      padding: 150px 60px 180px;
      width: 470px;
      min-height: 100%;
      background-color: #fff;
    }
    .login-title {
      font-size: 16px;
    }
    .login-btn-submit {
      width: 100%;
      margin-top: 38px;
    }
    .login-captcha {
      overflow: hidden;
      > img {
        width: 100%;
        cursor: pointer;
      }
    }
  }
</style>
